<?php namespace App\Models;

class ProductCategory extends \Eloquent
{
	protected $table = "product_categories";
	protected $softDelete = true;

	public static function getStructuredList($parentid=0, $class="blocklist", $id="productcategorylist", $prepend="/products/")
	{
		$html = "<ul class='$class' id='$id-$parentid'>";

		$subs = ProductCategory::with("products")->where("parentid", $parentid)->orderBy("displayorder")->get();

		foreach ($subs as $child) {
			$current_id = $child->id;
			$html .= "<li data-productcategoryid='$current_id' class='productcategorylistitem'>";
			$html .= "<a href='{$prepend}{$child->slug}'>{$child->title}</a>";

			if(ProductCategory::where("parentid", $current_id)->count() > 0)
			{
				$html .= ProductCategory::getStructuredList($current_id, $class, $id, $prepend);
			}

			$html .= "</li>";
		}
		$html .= "</ul>";
		return $html;
	}

	public static function getCategoryListMenu($parentid=0, $class="blocklist", $id="productcategorylist", $prepend="/products/")
	{
		$html = "<div class='$class' id='$id-$parentid'>";

		$subs = ProductCategory::with("products")->where("parentid", $parentid)->orderBy("displayorder")->get();

		foreach ($subs as $child) {
			$current_id = $child->id;
			$html .= "<div data-productcategoryid='$current_id' class='productcategorylistitem'>";
			$html .= "<a href='{$prepend}{$child->slug}'>{$child->title}</a>";

			if(ProductCategory::where("parentid", $current_id)->count() > 0)
			{
				$html .= ProductCategory::getCategoryListMenu($current_id, $class, $id, $prepend);
			}

			$html .= "</div>";
		}
		$html .= "</div>";
		return $html;
	}

	public function products()
	{
		return $this->belongsToMany("App\Models\Product", "products_product_categories", "categoryid", "productid");
	}
}